// pages/user/update_blind_date.js
import { User } from "../../model/user";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    realAuthType: false,
    xlAuth: false,
    phoneAuth: false,
    nameStatus: false,
    name: '',
    income: false,
    occupation: false,
    height_index: 0,
    height: ["请选择身高","150","160","170","180","190"],
    education_index: 0,
    education: ["请选择学历","专科及以下","本科","硕士","博士(含博士后)"],
    marriage: [],
    constellation: [],
    wages: ["请选择月收入","3k","5k","10k","15k","20k+"],
    wages_index: 0,
    car: false,
    room: [],
    unit: [],
    problem: '',
    sub_loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中',
    });
    this.getUserInfo();
  },
  /**
   * 获取用户信息
   */
  async getUserInfo() {
    const res = await User.getUserInfo();
    const userPrivacy = res.user_privacy;
    let realAuthType = false;
    if(userPrivacy.real_auth_type == 2) {
      realAuthType = true;
    }
    let educationAuthType = false;
    if(userPrivacy.xl_auth == 2) {
      educationAuthType = true;
    }
    let phoneType = false;
    if(userPrivacy.phone_type == 2) {
      phoneType = true;
    }
    let nameType = false;
    let nickname = '';
    if(userPrivacy.name_type == 2) {
      nameType = true;
      nickname = userPrivacy.nickname;
    }
    let incomeType = false;
    if(userPrivacy.income_type == 2) {
      incomeType = true;
    }
    let occupationType = false;
    if(userPrivacy.occupation_type == 2) {
      occupationType = true;
    }
    // 身高
    let height_index = 0;
    if(userPrivacy.height) {
      this.data.height.forEach((item,index)=>{
        if(item == userPrivacy.height) {
          height_index = index;
        }
      })
    }
    // 学历
    let education_index = 0;
    this.data.education.forEach((item,index) => {
        if(index == userPrivacy.education) {
          education_index = index;
        }
    });
    let wages_index = 0;
    this.data.wages.forEach((item,index) => {
        if(item == userPrivacy.wages) {
          wages_index = index;
        }
    })
    // 车
    let car = false;
    if(userPrivacy.car_type == 2) {
      car = true;
    }
    this.setData({
      realAuthType: realAuthType,
      xlAuth: educationAuthType,
      phoneAuth: phoneType,
      nameStatus: nameType,
      name: nickname,
      income: incomeType,
      occupation: occupationType,
      height_index: height_index,
      wages_index: wages_index,
      education_index: education_index,
      marriage: userPrivacy.marriage,
      constellation: userPrivacy.constellation,
      car: car,
      room: userPrivacy.room,
      unit: userPrivacy.unit,
      problem: userPrivacy.problem,
      userInfo: res
    });
    wx.hideLoading();
  },
  /**
   * 更新提交信息
   */
  async updateData() {
    const data = this.data;
    const self = this;
    let height = data.height[data.height_index];
    if(height == '请选择身高') {
      height = '';
    }
    let  wages = data.wages[data.wages_index];
    if(wages == '请选择月收入') {
      wages = '';
    }
    const dataSub = {
      realAuthType: data.realAuthType,
      educationAuthType: data.xlAuth,
      phoneType: data.phoneAuth,
      nameType: data.nameStatus,
      nickname: data.name,
      incomeType: data.income,
      occupationType: data.occupation,
      height: height,
      wages: wages,
      education: data.education_index,
      marriage: data.marriage,
      constellation: data.constellation,
      car: data.car,
      room: data.room,
      unit: data.unit,
      problem: data.problem,
    }
    if(data.problem.length > 100) {
      wx.showToast({
        title: '问题最长不超过100字符',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      sub_loading: true
    })
    const res = await User.updateUserPrivacy(dataSub);
    if(res) {
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
      self.setData({
        sub_loading: false
      })
      return false;
    } else {
      self.setData({
        sub_loading: false
      })
    }
  },
  /**
   *  实名认证开关
   */
  changeRealAuthType({ detail }) {
    this.setData({ realAuthType: detail });
  },
  /**
   *  学历认证开关
   */
  changeXlAuth({ detail }) {
    this.setData({ xlAuth: detail });
  },
  /**
   *  手机认证开关
   */
  changePhoneAuth({ detail }) {
    this.setData({ phoneAuth: detail });
  },
  /**
   * 车开关
   */
  changeCar({ detail }) {
    this.setData({ car: detail });
  },
  /**
   *  姓名开关
   */
  changeNameStatus({ detail }) {
    this.setData({ nameStatus: detail });
  },
  /**
   * 改变姓名
   */
  onChangeName(event) {
    this.setData({
      name: event.detail.value,
    })
  },
  /**
   * 改变问题
   */
  onChangeProblem(event) {
    this.setData({
      problem: event.detail.value,
    })
  },
  /**
   * 改变收入
   */
  changeIncome({ detail }) {
    this.setData({ income: detail });
  },
  /**
   * 改变职业
   */
  changeOccupation({ detail }) {
    this.setData({ occupation: detail });
  },
  /**
   * 身高选择
   */
  bindHeightChange(e) {
    this.setData({
      height_index: e.detail.value
    })
  },
  /**
   * 月收入选择
   */
  bindWagesChange(e) {
    this.setData({
      wages_index: e.detail.value
    })
  },
  /**
   * 学历
   */
  bindEducationChange(e) {
    this.setData({
      education_index: e.detail.value
    })
  },
  /**
   * 婚姻状态
   */
  onChangeMarriage(e) {
    let marriage = this.data.marriage;
    if(marriage.includes(e.currentTarget.dataset.id)) {
      let index_select = 0;
      marriage.forEach((item,index) => {
          if(item == e.currentTarget.dataset.id) {
            index_select = index;
          }
      })
      marriage.splice(index_select,1);
      this.setData({
        marriage: marriage
      });
    } else {
      marriage.push(e.currentTarget.dataset.id);
      this.setData({
        marriage: marriage
      });
    }
  },
  /**
   * 房状态
   */
  onChangeRoom(e) {
    let room = this.data.room;
    if(room.includes(e.currentTarget.dataset.id)) {
      let index_select = 0;
      room.forEach((item,index) => {
        if(item == e.currentTarget.dataset.id) {
          index_select = index;
        }
      })
      room.splice(index_select,1);
      this.setData({
        room: room
      });
    } else {
      room.push(e.currentTarget.dataset.id);
      this.setData({
        room: room
      });
    }
  },
  /**
   * 单位性质
   */
  onChangeUnit(e) {
    let unit = this.data.unit;
    if(unit.includes(e.currentTarget.dataset.id)) {
      let index_select = 0;
      unit.forEach((item,index) => {
        if(item == e.currentTarget.dataset.id) {
          index_select = index;
        }
      })
      unit.splice(index_select,1);
      this.setData({
        unit: unit
      });
    } else {
      unit.push(e.currentTarget.dataset.id);
      this.setData({
        unit: unit
      });
    }
  },
  /**
   * 选择星座
   */
  onChangeConstellation(e) {
    let constellation = this.data.constellation;
    if(constellation.includes(e.currentTarget.dataset.id)) {
      let index_select = 0;
      constellation.forEach((item,index) => {
        if(item == e.currentTarget.dataset.id) {
          index_select = index;
        }
      })
      constellation.splice(index_select,1);
      this.setData({
        constellation: constellation
      });
    } else {
      constellation.push(e.currentTarget.dataset.id);
      this.setData({
        constellation: constellation
      });
    }
  }
})